package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.entity.AutoBean;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.CustomerDetailBean;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class AutoVM extends TitleVM {
    public ObservableField<CustomerDetailBean.DataDTO> entity = new ObservableField<>();

    public AutoVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("自动匹配");
    }

    public ObservableList<AutoItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<AutoItemVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_auto);

    public ObservableField<Integer> showNoData=  new ObservableField<>(View.GONE);

    public void getMateInfo() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("minrent",entity.get().getMinRent());
        map.put("maxrent",entity.get().getMaxRent().replace("元","")+"");
        map.put("minarea",entity.get().getMinArea()+"");
        map.put("maxarea",entity.get().getMaxArea().replace("㎡","")+"");
        map.put("querters",entity.get().getQuarters()+"");

        model.mateinfo(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<AutoBean>() {
                    @Override
                    public void onNext(AutoBean o) {
                        if (o.issucc){
                            observableList.clear();
                            if (o.getData().size() > 0){
                                showNoData.set(View.GONE);
                                for (AutoBean.DataDTO bean : o.getData()) {
                                    AutoItemVM autoItemVM = new AutoItemVM(AutoVM.this,bean);
                                    observableList.add(autoItemVM);
                                }
                            }else {
                                showNoData.set(View.VISIBLE);
                            }
                        }else {
                            showNoData.set(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showNoData.set(View.VISIBLE);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
